package com.zxlspace.design.pattern.singleton;

/**
 * 单例模式
 * 饿汉式
 */
public class HungrySingleton {
    private final static HungrySingleton HUNGRY_SINGLETON;

    //static 静态代码块，类加载的时候直接实例化
    static {
        HUNGRY_SINGLETON = new HungrySingleton();
    }

    private HungrySingleton() {
        if (HUNGRY_SINGLETON != null) {
            throw new RuntimeException("单例构造器禁止反射调用");
        }
    }

    public static HungrySingleton getInstance() {
        return HUNGRY_SINGLETON;
    }

    //防止序列化攻击实例化
    private Object readResolve() {
        return HUNGRY_SINGLETON;
    }

}
